package com.demoo.zjsu.service.smartwebsite.jwc;

import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.service.smartwebsite.Page;
import com.demoo.zjsu.service.smartwebsite.PageFakeListFactoryExtend;
import com.demoo.zjsu.string.smartwebsite.WebsiteStrings;
import com.demoo.zjsu.util.smartwebsite.WebsiteUtil;
import com.demoo.zjsu.util.stringutil.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/18.
 * http://jww.zjgsu.edu.cn/index.asp
 * 教务处处理
 * 因为总是被防火墙拦截，考虑几个解决方案
 * 设置抽取时间
 */
@Service
public class JiaowuService extends PageFakeListFactoryExtend{

    @Override
    public List<Elements> getMainList(Document document) {
        List<Elements> elementsList = new ArrayList<Elements>();
        //教师专区
        Elements elements = document.select("[height=175]").eq(2).select("tr");
        //学生专区
        Elements elements1 = document.select("[height=175]").eq(1).select("tr");
        elementsList.add(elements);
        elementsList.add(elements1);
        //最新消息
        Elements elements2 = document.select("[width=410]").eq(0).select("tr");
        elementsList.add(elements2);
        return elementsList;
    }

    @Override
    public List<String> getTimes(Elements elements) {
        List<String> times = new ArrayList<String>();
        for (Element element : elements) {
            String time = element.select("font").text();
            times.add(time);
        }
        return times;
    }

    @Override
    public PageTwo dealTitle(String url, String title) {
        String key = StringUtil.cutIpLastByChar(url, '=');
        title = title.trim().substring(1);
        return new PageTwo(title, "", key, 0);
    }

    @Override
    public Elements getOtherMain(Document document) {

        return document.select("table[cellpadding=2]");
    }

    @Override
    public void filterAgain(Elements elements) {
        elements.select("tr").eq(0).remove();
        elements.select("style").remove();
    }

    @Override
    public PageTwoContent getContent(Elements elements) {

        //这里睡眠0.5s
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Elements tr = elements.select("tr");
        String throat = tr.eq(0).text().replaceAll("已被浏览.*次", "");
        tr.eq(0).remove();
        PageTwoContent content = getPageTwoContent(elements,"<!--.*-->");
        content.setThroat(throat);
        return content;
    }

    @Override
    public Page getPage() {

        return new Page(WebsiteStrings.CATEGOIES,"http://jww.zjgsu.edu.cn/index.asp");
    }

    @Override
    public void addFile(Elements elements) {
        elements.addAll(elements.select("a[href$=.doc]"));
        elements.addAll(elements.select("a[href$=.xls]"));
    }

    @Override
    public void packageContent(Elements main, List<PageTwo> pageTwos, List<PageTwoContent> contents, String category) {
        List<String> times = getTimes(main);
        Elements a = main.select("a");
        int size = a.size();
        for (int i = 0; i < size; i++) {
            Elements aElement = a.eq(i);
            String time = times.get(i);
            String abs_url = aElement.attr("abs:href");
            String url = aElement.attr("href");
            String title = aElement.attr("title");
            Document document1 = WebsiteUtil.getDocByURL(abs_url);
            PageTwo pageTwo = dealTitle(url, title);
            pageTwo.setDate(time);
            pageTwo.setCategory(category);
            String key = pageTwo.getKey();
            Elements elements = getOtherMain(document1);
            filterAgain(elements);
            filterMain(elements);
            dealPhotoAndFile(elements);
            PageTwoContent content = getContent(elements);
            content.setKey(key);
            contents.add(content);
            pageTwos.add(pageTwo);
        }
    }

}
